package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.SSrvsCronConf;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时调度配置实体类-[表名: 定时调度配置实体类(s_srvs_cron_conf)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2017-10-30
 */
public class SSrvsCronConfDao {

	/* 连接对象 */
	Connection conn = null;	

	public SSrvsCronConfDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 查询列表
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public List<SSrvsCronConf> queryAll()throws SQLException {
		List<SSrvsCronConf> sSrvsCronConfList = new ArrayList<SSrvsCronConf>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM s_srvs_cron_conf");
			rs = ps.executeQuery();
			while (rs.next()) {
				SSrvsCronConf sSrvsCronConf = new SSrvsCronConf();
				sSrvsCronConf.setJobCode(rs.getString("job_code"));
				sSrvsCronConf.setJobDesc(rs.getString("job_desc"));
				sSrvsCronConf.setJobClassType(rs.getString("job_class_type"));
				sSrvsCronConf.setJobClass(rs.getString("job_class"));
				sSrvsCronConf.setJobMethod(rs.getString("job_method"));
				sSrvsCronConf.setJobState(rs.getString("job_state"));
				sSrvsCronConf.setCronExpression(rs.getString("cron_expression"));
				sSrvsCronConf.setAgainTime(rs.getInt("again_time"));
				sSrvsCronConf.setRetrySecond(rs.getInt("retry_second"));
				sSrvsCronConf.setRemark(rs.getString("remark"));
				sSrvsCronConf.setAgentId(rs.getString("agent_id"));
				sSrvsCronConf.setServiceVersion(rs.getString("service_version"));
				sSrvsCronConf.setServiceGroup(rs.getString("service_group"));
				sSrvsCronConf.setServiceTimeout(rs.getBigDecimal("service_timeout"));
				sSrvsCronConf.setSubsCode(rs.getString("subs_code"));
				sSrvsCronConf.setLastExecuteTime(rs.getString("last_execute_time"));
				sSrvsCronConf.setNextExecuteTime(rs.getString("next_execute_time"));
				sSrvsCronConf.setCostTime(rs.getBigDecimal("cost_time"));
				sSrvsCronConfList.add(sSrvsCronConf);
			}
		} catch (SQLException e) {
			throw new SQLException("queryAll is Wrong!"+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return sSrvsCronConfList;
	}
	
	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public SSrvsCronConf queryByPK(SSrvsCronConf cronConf) throws SQLException {
		SSrvsCronConf sSrvsCronConf = new SSrvsCronConf();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM s_srvs_cron_conf where job_code = ?");
			ps.setString(1, cronConf.getJobCode());
			rs = ps.executeQuery();
			while (rs.next()) {
				sSrvsCronConf.setJobCode(rs.getString("job_code"));
				sSrvsCronConf.setJobDesc(rs.getString("job_desc"));
				sSrvsCronConf.setJobClassType(rs.getString("job_class_type"));
				sSrvsCronConf.setJobClass(rs.getString("job_class"));
				sSrvsCronConf.setJobMethod(rs.getString("job_method"));
				sSrvsCronConf.setJobState(rs.getString("job_state"));
				sSrvsCronConf.setCronExpression(rs.getString("cron_expression"));
				sSrvsCronConf.setAgainTime(rs.getInt("again_time"));
				sSrvsCronConf.setRetrySecond(rs.getInt("retry_second"));
				sSrvsCronConf.setRemark(rs.getString("remark"));
				sSrvsCronConf.setAgentId(rs.getString("agent_id"));
				sSrvsCronConf.setServiceVersion(rs.getString("service_version"));
				sSrvsCronConf.setServiceGroup(rs.getString("service_group"));
				sSrvsCronConf.setServiceTimeout(rs.getBigDecimal("service_timeout"));
				sSrvsCronConf.setSubsCode(rs.getString("subs_code"));
				sSrvsCronConf.setLastExecuteTime(rs.getString("last_execute_time"));
				sSrvsCronConf.setNextExecuteTime(rs.getString("next_execute_time"));
				sSrvsCronConf.setCostTime(rs.getBigDecimal("cost_time"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryByPK is Wrong!"+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return sSrvsCronConf;
	}

	/**
	 * 根据服务节点查询单条记录
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public List<SSrvsCronConf> queryByAgentId(String agentId) throws SQLException {
		List<SSrvsCronConf> sSrvsCronConfList = new ArrayList<SSrvsCronConf>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM s_srvs_cron_conf where agent_id = ?");
			ps.setString(1, agentId);
			rs = ps.executeQuery();
			while (rs.next()) {
				SSrvsCronConf sSrvsCronConf = new SSrvsCronConf();
				sSrvsCronConf.setJobCode(rs.getString("job_code"));
				sSrvsCronConf.setJobDesc(rs.getString("job_desc"));
				sSrvsCronConf.setJobClassType(rs.getString("job_class_type"));
				sSrvsCronConf.setJobClass(rs.getString("job_class"));
				sSrvsCronConf.setJobMethod(rs.getString("job_method"));
				sSrvsCronConf.setJobState(rs.getString("job_state"));
				sSrvsCronConf.setCronExpression(rs.getString("cron_expression"));
				sSrvsCronConf.setAgainTime(rs.getInt("again_time"));
				sSrvsCronConf.setRetrySecond(rs.getInt("retry_second"));
				sSrvsCronConf.setRemark(rs.getString("remark"));
				sSrvsCronConf.setAgentId(rs.getString("agent_id"));
				sSrvsCronConf.setServiceVersion(rs.getString("service_version"));
				sSrvsCronConf.setServiceGroup(rs.getString("service_group"));
				sSrvsCronConf.setServiceTimeout(rs.getBigDecimal("service_timeout"));
				sSrvsCronConf.setSubsCode(rs.getString("subs_code"));
				sSrvsCronConf.setLastExecuteTime(rs.getString("last_execute_time"));
				sSrvsCronConf.setNextExecuteTime(rs.getString("next_execute_time"));
				sSrvsCronConf.setCostTime(rs.getBigDecimal("cost_time"));
				sSrvsCronConfList.add(sSrvsCronConf);
			}
		} catch (SQLException e) {
			throw new SQLException("queryByAgentId is Wrong!"+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return sSrvsCronConfList;
	}
	
	/**
	 * 根据主键更新
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int updateByPk(SSrvsCronConf sSrvsCronConf){
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("UPDATE s_srvs_cron_conf SET"
					+ " job_code = ? ,"
					+ " job_desc = ? ,"
					+ " job_class_type = ? ,"
					+ " job_class = ? ,"
					+ " job_method = ? ,"
					+ " service_version = ? ,"
					+ " service_group = ? ,"
					+ " service_timeout = ? ,"
					+ " cron_expression = ? ,"
					+ " again_time = ? ,"
					+ " retry_second = ? ,"
					+ " agent_id = ? ,"
					+ " subs_code = ? ,"
					+ " remark = ? ,"
					+ " job_state = ? ,"
					+ " last_execute_time = ? ,"
					+ " next_execute_time = ? ,"
					+ " cost_time = ? "
					+"WHERE job_code=?");
			ps.setObject(1, sSrvsCronConf.getJobCode());
			ps.setObject(2, sSrvsCronConf.getJobDesc());
			ps.setObject(3, sSrvsCronConf.getJobClassType());
			ps.setObject(4, sSrvsCronConf.getJobClass());
			ps.setObject(5, sSrvsCronConf.getJobMethod());
			ps.setObject(6, sSrvsCronConf.getServiceVersion());
			ps.setObject(7, sSrvsCronConf.getServiceGroup());
			ps.setObject(8, sSrvsCronConf.getServiceTimeout());
			ps.setObject(9, sSrvsCronConf.getCronExpression());
			ps.setObject(10, sSrvsCronConf.getAgainTime());
			ps.setObject(11, sSrvsCronConf.getRetrySecond());
			ps.setObject(12, sSrvsCronConf.getAgentId());
			ps.setObject(13, sSrvsCronConf.getSubsCode());
			ps.setObject(14, sSrvsCronConf.getRemark());
			ps.setObject(15, sSrvsCronConf.getJobState());
			ps.setObject(16, sSrvsCronConf.getLastExecuteTime());
			ps.setObject(17, sSrvsCronConf.getNextExecuteTime());
			ps.setObject(18, sSrvsCronConf.getCostTime());
			ps.setObject(19, sSrvsCronConf.getJobCode());
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.getStackTrace();
		} finally {
			close(rs, null, ps);
		}
		return  0;
	}
	
	/**
	 * 新增
	 * @param sSrvsCronConf
	 * @return
	 * @throws SQLException
	 */
	public int insertSSrvsCronConf(SSrvsCronConf sSrvsCronConf) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("insert into s_srvs_cron_conf (" +
					"job_code,job_desc,job_class_type,job_class,job_method,service_version,service_group,service_timeout,cron_expression,again_time,retry_second,agent_id,subs_code,remark,job_state,last_execute_time,next_execute_time, cost_time" +
					") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			ps.setObject(1, sSrvsCronConf.getJobCode());
			ps.setObject(2, sSrvsCronConf.getJobDesc());
			ps.setObject(3, sSrvsCronConf.getJobClassType());
			ps.setObject(4, sSrvsCronConf.getJobClass());
			ps.setObject(5, sSrvsCronConf.getJobMethod());
			ps.setObject(6, sSrvsCronConf.getServiceVersion());
			ps.setObject(7, sSrvsCronConf.getServiceGroup());
			ps.setObject(8, sSrvsCronConf.getServiceTimeout());
			ps.setObject(9, sSrvsCronConf.getCronExpression());
			ps.setObject(10, sSrvsCronConf.getAgainTime());
			ps.setObject(11, sSrvsCronConf.getRetrySecond());
			ps.setObject(12, sSrvsCronConf.getAgentId());
			ps.setObject(13, sSrvsCronConf.getSubsCode());
			ps.setObject(14, sSrvsCronConf.getRemark());
			ps.setObject(15, sSrvsCronConf.getJobState());
			ps.setObject(16, sSrvsCronConf.getLastExecuteTime());
			ps.setObject(17, sSrvsCronConf.getNextExecuteTime());
			ps.setObject(18, sSrvsCronConf.getCostTime());
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert SSrvsCronConf is Wrong!" + e.getMessage());
		} finally {
			close(rs, null, ps);
		}
	}

	/**
	 * 根据主键删除一行记录
	 * @return int
	 */
	public int deleteByPk(SSrvsCronConf sSrvsCronConf) throws SQLException {

		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("delete from s_srvs_cron_conf where 1=1"
					+ " AND job_code = ? "
			);
			ps.setObject(1, sSrvsCronConf.getJobCode());
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete SSrvsCronConf is Wrong!" + e.getMessage());
		} finally {
			close(rs, null, ps);
		}
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
